﻿using System;
using System.ComponentModel;
using System.Data;
using DevExpress.XtraGrid.Views.BandedGrid;


namespace Marmat.Forms.Views
{
    namespace DataGrid
    {
        public partial class Column
        {

            // <Category("数据编辑"), Description("获取或设置是否允许编辑此列。"),
            // Localizable(True), RefreshProperties(RefreshProperties.All), DefaultValue(True)>
            // Public Property AllowEdit As Boolean
            // Get
            // Return m_AllowEdit
            // End Get
            // Set(value As Boolean)
            // m_AllowEdit = value

            // If Me.GridColumn IsNot Nothing Then
            // With Me.GridColumn
            // .OptionsColumn.AllowEdit = value
            // End With
            // End If
            // End Set
            // End Property
            // Private m_AllowEdit As Boolean = True

            // Private _Caption As String
            // ''' <summary>
            // ''' 获取或设置列的标题
            // ''' </summary>
            // ''' <value></value>
            // ''' <returns></returns>
            // ''' <remarks></remarks>
            // <Category("外观"), Description("获取或设置列的标题。"),
            // Localizable(True), RefreshProperties(RefreshProperties.All)>
            // Public Property Caption As String
            // Get
            // If Me._Caption Is Nothing OrElse Me._Caption = "" Then
            // Return Me.ColumnName
            // End If
            // Return Me._Caption
            // End Get
            // Set(value As String)
            // Me._Caption = value
            // If Me.GridColumn IsNot Nothing Then
            // Me.DataColumn.Caption = Me.Caption
            // End If
            // End Set
            // End Property


            // ''' <summary>
            // ''' 字符型，返回或设置列的默认值表达式。
            // ''' </summary>
            // ''' <value></value>
            // ''' <returns></returns>
            // ''' <remarks></remarks>
            // <DefaultValue(CStr(Nothing))>
            // Public Property DefaultValue As String
            // Get
            // Return Me.m_DefaultValue
            // End Get
            // Set(value As String)
            // Me.m_DefaultValue = value
            // End Set
            // End Property
            // Private m_DefaultValue As String

            // <DefaultValue(98)>
            // Public Property Width As Integer
            // Get
            // Return Me.m_Width
            // End Get
            // Set(value As Integer)
            // Me.m_Width = value
            // If Me.GridColumn IsNot Nothing Then
            // Me.GridColumn.Width = Me.Width
            // End If
            // End Set
            // End Property
            // Private m_Width As Integer = 98

            // <Bindable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", GetType(UITypeEditor))>
            // Public ReadOnly Property Items As List(Of Object)
            // Get
            // If m_Items Is Nothing Then
            // m_Items = New List(Of Object)
            // End If
            // Return Me.m_Items
            // End Get
            // End Property
            // Private m_Items As List(Of Object)

            // <DefaultValue(CStr(Nothing))>
            // Public Property PopupControl As PopupControl

            // <DefaultValue(GetType(ColumnEditEnum), "None")>
            // Public Property ColumnEdit As ColumnEditEnum
            // Get
            // Return Me.m_ColumnEdit
            // End Get
            // Set(value As ColumnEditEnum)
            // If m_ColumnEdit <> value Then
            // m_ColumnEdit = value
            // Select Case Me.ColumnEdit
            // Case ColumnEditEnum.None
            // Me.GridColumn.ColumnEdit = Nothing
            // Case ColumnEditEnum.ComboList
            // If Me.GridColumn.ColumnEdit IsNot Nothing Then
            // Me.GridColumn.ColumnEdit.Dispose()
            // End If
            // Me.GridColumn.ColumnEdit = New RepositoryItemComboBox
            // CType(Me.GridColumn.ColumnEdit, RepositoryItemComboBox).Items.AddRange(Me.Items.ToArray)

            // End Select

            // End If
            // End Set
            // End Property
            // Private m_ColumnEdit As ColumnEditEnum


            /// <summary>
            /// 如果是字符列，返回该列允许输入的最大长度，否则返回-1
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            [Browsable(true)]
            [Category("数据类型")]
            [Description("如果是字符列, 返回该列允许输入的最大长度, 否则返回 - 1")]
            public int MaxLength
            {
                get
                {
                    if (ReferenceEquals(this.DataType, typeof(string)))
                    {
                        return this._MaxLength;
                    }

                    return -1;
                }

                set
                {
                    this._MaxLength = value;
                    // If Me.DataColumn IsNot Nothing AndAlso Me.DataType Is GetType(String) Then
                    // Me.DataColumn.MaxLength = MaxLength
                    // End If
                }
            }

            private int _MaxLength = -1;
            private Type _DataType = typeof(string);
            /// <summary>
            /// 获取或设置存储在列中的类型
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            [DefaultValue(typeof(string))]
            [TypeConverter(typeof(DataTypeConverter))]
            [Category("数据类型")]
            [Description("获取或设置存储在列中的类型。")]
            public override Type DataType
            {
                get
                {
                    return this._DataType;
                }

                set
                {
                    if (value is object)
                    {
                        this._DataType = value;
                        if (this.GridColumn is object)
                        {
                        }
                    }
                }
            }

            /// <summary>
            /// 获取或设置此列是否属于主键列。
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            [DefaultValue(false)]
            [Category("数据类型")]
            [Description("获取或设置此列是否属于主键列。")]
            public bool PrimaryKey { get; set; }

            /// <summary>
            /// 获取或设置该列是否为临时表达式列。
            /// </summary>
            [DefaultValue(false)]
            [Category("数据类型")]
            [Description("获取或设置该列是否为临时表达式列。")]
            public bool IsTemporary
            {
                get => this._IsTemporary;
                set
                {
                    this._IsTemporary = value;
                    if (this.IsTemporary && !string.IsNullOrEmpty(this.Expression))
                    {
                        this.Expression = this.Expression;
                    }
                }
            }

            private bool _IsTemporary;


            /// <summary>
            /// 字符型，返回或设置列的默认值表达式。
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            [DefaultValueAttribute(null)]
            public string DefaultValue
            {
                get
                {
                    return this.m_DefaultValue;
                }

                set
                {
                    this.m_DefaultValue = value;
                }
            }

            private string m_DefaultValue;

            [Browsable(false)]
            public bool IsNumeric
            {
                get
                {
                    switch (this.DataType)
                    {
                        case var @case when @case == typeof(int):
                        case var case1 when case1 == typeof(float):
                        case var case2 when case2 == typeof(double):
                        case var case3 when case3 == typeof(decimal):
                            {
                                return true;
                            }

                        default:
                            {
                                return false;
                            }
                    }
                }
            }

            /// <summary>
            /// 获取或设置一个值，该值指示对于添加到该表中的新行，列是否将列的值自动递增。
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            internal bool AutoIncrement
            {
                get
                {
                    return this._AutoIncrement;
                }

                set
                {
                    if (this.AutoIncrement != value)
                    {
                        this._AutoIncrement = value;
                        if (this.DataColumn is object)
                        {
                            this.DataColumn.AutoIncrement = value;
                        }
                    }
                }
            }

            private bool _AutoIncrement = false;

            ///// <summary>
            ///// 设置数值列允许输入的最大小数位数。
            ///// </summary>
            ///// <returns></returns>
            //[DefaultValue(0)]
            //public new int Decimals { get; set; } = 4;

            [DefaultValue(true)]
            public new bool Visible
            {
                get
                {
                    return base.Visible;
                }

                set
                {
                    base.Visible = value;
                    if (this.GridBand is object)
                    {
                        // Me.GridBand.Visible = value
                        this.ParentBand((GridBand)this.GridBand, value);
                    }

                    if (this.GridColumn is object)
                    {
                        this.GridColumn.Visible = value;
                    }
                }
            }

            [DefaultValue(null)]
            [Browsable(false)]
            public DataColumn DataColumn { get; set; }
            private object GridBand { get; set; }

            private void ParentBand(GridBand GridBand, bool Visible)
            {
                GridBand.Visible = Visible;
                if (GridBand.ParentBand is object)
                {
                    bool V = Visible;
                    foreach (BandedGridColumn Item in GridBand.ParentBand.Columns)
                    {
                        if (Item.Visible == true)
                        {
                            V = true;
                            break;
                        }
                    }

                    if (V == false)
                    {
                        foreach (GridBand Item in GridBand.ParentBand.Children)
                        {
                            if (Item.Visible == true)
                            {
                                V = true;
                                break;
                            }
                        }
                    }

                    this.ParentBand(GridBand.ParentBand, V);
                }
            }

            [Category("数据编辑")]
            [Description("获取或设置列的名称。")]
            [Localizable(true)]
            [RefreshProperties(RefreshProperties.All)]
            [DefaultValue("")]
            public override string Name
            {
                get
                {
                    return base.Name;
                }

                set
                {
                    if (this.DataColumn is object)
                    {
                        {
                            var withBlock = this.DataColumn;
                            withBlock.ColumnName = value;
                        }
                    }

                    base.Name = value;
                    // If Me.GridColumn IsNot Nothing Then
                    // With Me.GridColumn
                    // .FieldName = value
                    // .Name = value
                    // End With
                    // End If
                }
            }

            /// <summary>
            /// 返回或设置列的计算表达式。
            /// 只有表达式列或者通过代码增加的临时列，才可以设置Expression属性。
            /// </summary>
            [DefaultValue(null)]
            [Category("数据编辑")]
            [Description("返回或设置列的计算表达式。只有表达式列或者通过代码增加的临时列，才可以设置Expression属性。")]
            public override string Expression
            {
                get
                {
                    return this._expression;
                }

                set
                {
                    this._expression = value;
                    if (this.IsTemporary)
                    {
                        if (this.DataColumn is object)
                        {
                            this.DataColumn.Expression = this.Expression;
                        }
                    }
                }
            }

            private string _expression;

            // ''' <summary>
            // ''' 如果是字符列，返回该列允许输入的最大长度，否则返回-1
            // ''' </summary>
            // ''' <value></value>
            // ''' <returns></returns>
            // ''' <remarks></remarks>
            // <Browsable(True), _
            // Category("数据类型"), Description("如果是字符列, 返回该列允许输入的最大长度, 否则返回 - 1")>
            // Public Property MaxLength As Integer
            // Get
            // If Me.DataType Is GetType(String) Then
            // Return Me._MaxLength
            // End If
            // Return -1
            // End Get
            // Set(value As Integer)
            // Me._MaxLength = value
            // If Me.DataColumn IsNot Nothing AndAlso Me.DataType Is GetType(String) Then
            // Me.DataColumn.MaxLength = MaxLength
            // End If
            // End Set
            // End Property
            // Private _MaxLength As Integer = 32

            // Private _DataType As Type = GetType(System.String)
            // ''' <summary>
            // ''' 获取或设置存储在列中的类型
            // ''' </summary>
            // ''' <value></value>
            // ''' <returns></returns>
            // ''' <remarks></remarks>
            // <DefaultValue(GetType(System.String)), TypeConverter(GetType(DataTypeConverter)),
            // Category("数据类型"), Description("获取或设置存储在列中的类型。")>
            // Public Property DataType As Type
            // Get
            // Return Me._DataType
            // End Get
            // Set(value As Type)
            // If value IsNot Nothing Then
            // Me._DataType = value
            // If Me.DataColumn IsNot Nothing Then
            // Me.DataColumn.DataType = value
            // End If
            // End If
            // End Set
            // End Property
        }
    }
}